package com.bz.xtcx.manager.controller;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.CmsFile;
import com.bz.xtcx.manager.entity.CmsFloatBox;
import com.bz.xtcx.manager.entity.User;
import com.bz.xtcx.manager.service.ICmsFloatBoxService;
import com.bz.xtcx.manager.utils.MyFileUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoQueryExchanges;
import com.bz.xtcx.manager.vo.VoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ygl
 * @create 2018-11-08
 * @DESCRIPTION
 **/
@RestController
@RequestMapping("cmsFloatBox")
public class CmsFloatBoxController extends BaseController {

    @Autowired
    private ICmsFloatBoxService cmsFloatBoxService;


    @Value("${file.tomcatPath}")
    private String tomcatPath;

    @PostMapping("/save")
    public VoResponse save(@RequestBody CmsFloatBox model) {
        VoResponse voRes = new VoResponse();
        // 新增
        if (StringUtils.isEmpty(model.getId())) {
            model.setId(UUID.randomUUID().toString());
            model.setCreater(cmsFloatBoxService.getUserName());
            model.setCreateTime(new Date());
            model.setUpdater(cmsFloatBoxService.getUserName());
            model.setUpdateTime(new Date());
            cmsFloatBoxService.insert(model);
            voRes.setData(model);
        } else {
            model.setUpdater(cmsFloatBoxService.getUserName());
            model.setUpdateTime(new Date());
            cmsFloatBoxService.update(model);
            voRes.setData(model);
        }
        return voRes;
    }

    @DeleteMapping("/{id}")
    public VoResponse delete(@PathVariable String id) {
        VoResponse voRes = new VoResponse();
        cmsFloatBoxService.delete(id);
        return voRes;
    }

    @GetMapping("/{id}")
    public VoResponse get(@PathVariable String id) {
        VoResponse voRes = new VoResponse();
        CmsFloatBox cmsFloatBox = cmsFloatBoxService.selectById(id);
        voRes.setData(cmsFloatBox);
        return voRes;
    }

    @GetMapping("page")
    public VoResponse getAll(@RequestParam("content") String content,@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
                                      @RequestParam(value="orderBy",required=false)  String orderBy) {
        VoResponse voRes = new VoResponse();
        try {
            voRes.setData(cmsFloatBoxService.getPageByCondition(content,pageNum, pageSize, orderBy));
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("查询错误:" + e.getMessage());
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("查询错误:" + e.getMessage());
        }
        return voRes;
    }

    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) {
        MyFileUtil util = new MyFileUtil();
        String path = "content/";
        String filePath = tomcatPath + path;
        VoResponse voRes = null;
        String fileName = file.getOriginalFilename();
        String [] suffixNames = {".jpg", ".jpeg", ".png"};
        voRes = util.upload(file, filePath, suffixNames);
        CmsFile cmsFile = new CmsFile();
        cmsFile.setFileName(fileName);
        cmsFile.setSavePath(path + voRes.getData());
        voRes.setData(cmsFile);
        return voRes;
    }

    @GetMapping("/file/delete")
    public Object deleteFile(@RequestParam("fileName") String fileName) {
        VoResponse voRes = new VoResponse();
        MyFileUtil util = new MyFileUtil();
        String url = tomcatPath + fileName;
        util.delFile(url);
        return voRes;
    }

    /**
     * 门户获取悬浮窗信息
     * @return
     */
    @GetMapping("/box")
    public Object box() {
        VoResponse voRes = new VoResponse();
        List<CmsFloatBox> boxList = cmsFloatBoxService.getBox();
        voRes.setData(boxList);
        return voRes;
    }
}
